Page({
  data: {
    userInfo: {},
    foods: [] ,// 用于存储食物列表,
    selectCategory:1,
  },
  redirectTo: function(event) {
    const url = event.currentTarget.dataset.url;
    wx.redirectTo({
      url: url
    });
  },
  navigateToDetails: function(event) {
    // 获取传递的id
    const id = event.currentTarget.dataset.id;
    console.log(id);
    // 跳转到详情页面，将id作为参数传递
    wx.navigateTo({
      url: '/pages/introduce/introduce?id=' + id
    });
  },
  onLoad(option) {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    this.init();
    this.getFoodsByCategory(1);
  },
  async init() {
    wx.cloud.init(); // 初始化云能力
    // 这里可以添加其他初始化逻辑
  },
  onTitleTap: function(event) {
    const category = parseInt(event.currentTarget.dataset.category, 10);
    this.setData({
      selectCategory: category
    });
    console.log(category);
    this.getFoodsByCategory(category);
  },
  async getFoodsByCategory(category) {
    try {
      const res = await wx.cloud.callFunction({
        name: 'classification',
        data: {
          category: category
        }
      });
      if (res.result.code === 0) {
        this.setData({
          foods: res.result.data
        });
        console.log(this.data.foods); // 打印更新后的食物列表数据
      } else {
        console.error('获取食物列表失败:', res.result.msg);
      }
    } catch (err) {
      console.error('调用云函数时发生错误:', err);
    }
  },
});
const app=getApp()